/*******************************************************************************
 * Copyright (c) 2014 Francesco Gabbrielli
 * 
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v2.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Contributors:
 *     Francesco Gabbrielli - initial API and implementation
 ******************************************************************************/
package it.francescogabbrielli.android.solidalapp.file;

import com.actionbarsherlock.app.SherlockDialogFragment;

import it.francescogabbrielli.android.solidalapp.Action;
import it.francescogabbrielli.android.solidalapp.Constants;
import it.francescogabbrielli.android.solidalapp.DataEvent;
import it.francescogabbrielli.android.solidalapp.EndpointsLoader;
import it.francescogabbrielli.android.solidalapp.EndpointsUtils;
import it.francescogabbrielli.android.solidalapp.R;
import it.francescogabbrielli.android.solidalapp.StatusEvent;
import it.francescogabbrielli.android.solidalapp.SolidalApp.DataKey;
import it.francescogabbrielli.android.solidalapp.actions.EndpointsUpdateResponse;
import it.francescogabbrielli.android.solidalapp.actions.GCMInsertRequestMessage;
import it.francescogabbrielli.android.solidalapp.requestendpoint.model.Request;
import it.francescogabbrielli.android.solidalapp.responseendpoint.model.Response;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.Loader;
import android.view.View;

public class RequestFileActivity extends FileActivity<Response> 
		implements LoaderCallbacks<Response> {
	
	@Override
	public void onStatusUpdate(StatusEvent event) {
		
	}
	
	@Override
	public void onDataChanged(DataEvent event) {
	}
	
	@Override
	protected String getProfileFragmentName() {
		return RequestFileFragment.class.getName();
	}
		
	@Override
	public DataKey getChatKey() {
		return DataKey.CHAT_REQUEST;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Request request = (Request) app.getData(DataKey.REQUEST);
		if (request!=null)
			reload(app.getData(DataKey.FILE_RESPONSE)==null, request.getId(), Constants.LOADER_FILE_RESPONSE);
	}
	
	@Override
	public Loader<Response> onCreateLoader(int id, Bundle data) {
		return new EndpointsLoader<Response>(this, Response.class)
				.setFilters(data)
				.setMethod("getRequestResponse");
	}

	@Override
	public void onLoadFinished(Loader<Response> loader, Response response) {
		app.setData(DataKey.FILE_RESPONSE, response);
		if (response!=null) {
			setTitle(getString(R.string.title_activity_file, response.getId()));
		} else
			app.alert(getString(R.string.error_server, "Null Response?"));
	}

	@Override
	public void onLoaderReset(Loader<Response> loader) {
	}
	
	public void onFeedbackOk(View view) {
		showConfirmFeedback(1);
	}

	public void onFeedbackKo(View view) {
		showConfirmFeedback(-1);
	}
	
	class ConfirmDialog extends SherlockDialogFragment {
		private int feedback;
		public ConfirmDialog setFeedback(int feedback) {
			this.feedback = feedback;
			return this;
		}
		@Override
		public Dialog onCreateDialog(Bundle savedInstanceState) {
			AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
			return builder
				.setTitle("Conferma feedback?")
				.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						Response res = (Response) app.getData(DataKey.FILE_RESPONSE);
						Bundle data = EndpointsUtils.toBundle(res);
						data.putInt(Constants.FEEDBACK, feedback);
						Action.start(RequestFileActivity.this, EndpointsUpdateResponse.class, data);
					}
				})
				.create();
		}
	}
	
	ConfirmDialog confirmDialog;
	
	private void showConfirmFeedback(int feedback) {
		if (confirmDialog==null)
			confirmDialog = new ConfirmDialog();
		confirmDialog.setFeedback(feedback).show(getSupportFragmentManager(), "confirm");
	}
	
	@Override
	public void sendMessage(long id, String message) {
		if (app.getData(DataKey.FILE_RESPONSE_PROFILE)!=null) {
			Response res = (Response) app.getData(DataKey.FILE_RESPONSE);
			Bundle data = EndpointsUtils.toBundle(res);
			data.putLong("id", id);
			data.putString("message", message);
			Action.start(this, GCMInsertRequestMessage.class, data);
		} else
			app.alert(R.string.file_chat_not_connected);
	}
		
	@Override
	protected void onResume() {
		super.onResume();
	}
}
